package com.ciaojian.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ciaojian.client.service.StoreServiceImpl;
import com.ciaojian.client.service.UserOpinionServiceImpl;
import com.ciaojian.client.service.WorkerServiceImpl;
import com.ciaojian.core.enums.business.WorkerStatusEnum;
import com.ciaojian.core.model.Store;
import com.ciaojian.core.model.UserOpinion;
import com.ciaojian.core.model.Worker;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Atlakyn
 */
@Api(tags = "用户反馈")
@RestController
@RequestMapping("/user-opinion")
public class UserOpinionController {
    @Resource
    private UserOpinionServiceImpl userOpinionService;
    @Resource
    private StoreServiceImpl storeService;
    @Resource
    private WorkerServiceImpl workerService;

    /**
     * 添加反馈信息
     *
     * @param userOpinion 反馈记录
     * @return 反馈结果
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("添加反馈信息")
    @PostMapping
    public Result insert(@RequestBody UserOpinion userOpinion) {
        if (userOpinionService.save(userOpinion)) {
            return Result.ok(userOpinion);
        } else {
            return Result.error("添加失败");
        }
    }

    @ApiOperation("获取门店")
    @GetMapping("/stores")
    public Result getStoreList() {
        LambdaQueryWrapper<Store> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Store::getId, Store::getTitle);
        lambdaQueryWrapper.eq(Store::getBusinessType, 0);
        return Result.ok(storeService.listMaps(lambdaQueryWrapper));
    }

    @ApiOperation("获取门店下的发型师")
    @GetMapping("/workers")
    public Result getWorkerList(Integer storeId) {
        LambdaQueryWrapper<Worker> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Worker::getId, Worker::getName);
        lambdaQueryWrapper.ne(Worker::getStatus, WorkerStatusEnum.outgoing)
                .eq(Worker::getStoreId, storeId);
        return Result.ok(workerService.listMaps(lambdaQueryWrapper));
    }
}
